/*
\u4f7f\u7528\u65b9\u6cd5:
\u5728\u7db2\u9801\u4e2d\u589e\u52a0\u4e00\u884c:
<script language="JavaScript" src="../../html/erpjsca.js"></script>

\u63a5\u6536\u65e5\u671f\u7684\u8f38\u5165\u6b04\u4f4d:
  <input type="text" name="Date" id="AppDate" size="12" maxlength="10" value="">
\u986f\u793a\u842c\u5e74\u66c6\u7684\u5716\u793a:
  <img src="../.../../Images/app_date.gif" alt="\u65e5\u671f\u63d0\u793a" onClick="showCalendar('AppDate')">                                              ^^^^^^^^^^^^^^^^^^^
                                             \u6b64\u70ba\u547c\u53eb\u7684 method , \u53c3\u6578\u70ba\u8f38\u5165\u6b04\u4f4d\u7684\u540d\u5b57
*/
// how reliable is this test?
isIE = (document.all ? true : false);

// Initialize arrays.
var ca_months = new Array("\u4e00\u6708","\u4e8c\u6708","\u4e09\u6708","\u56db\u6708","\u4e94\u6708","\u516d\u6708","\u4e03\u6708","\u516b\u6708","\u4e5d\u6708","\u5341\u6708","\u5341\u4e00\u6708", "\u5341\u4e8c\u6708");
var ca_daysInMonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var ca_displayMonth = new Date().getMonth();
var ca_displayYear = new Date().getFullYear();
var ca_displayDivName;
var ca_displayElement;
var old_colname;
document.write("<div id=Calendar style=\"position:absolute;border:2px outset;background-color:White;z-index:2;display:none;\"></div>");

function showCalendar(eltName) {
	document.all("Calendar").style.left=window.event.x-50;
	document.all("Calendar").style.top=window.event.y;
	newCalendar("Calendar",document.all(eltName));
	old_colname=eltName;
}

function getDays(month, year) {
	// Test for leap year when February is selected.
	if (1 == month)
		return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
	else
		return ca_daysInMonth[month];
}

function getToday() {
	// Generate today's date.
	this.now = new Date();
	this.year = this.now.getFullYear();
	this.month = this.now.getMonth();
	this.day = this.now.getDate();
}

// Start with a calendar for today.
today = new getToday();

function newCalendar(eltName,attachedElement) {
	if (attachedElement) {
		if (ca_displayDivName && ca_displayDivName != eltName)
			ca_hideElement(ca_displayDivName);
		ca_displayElement = attachedElement;
	}
	ca_displayDivName = eltName;
	today = new getToday();
	var parseYear = parseInt(ca_displayYear + '');
	var newCal = new Date(parseYear,ca_displayMonth,1);
	var day = -1;
	var startDayOfWeek = newCal.getDay();
	if ((today.year == newCal.getFullYear()) && (today.month == newCal.getMonth())) {
		day = today.day;
	}
	var intca_daysInMonth = getDays(newCal.getMonth(), newCal.getFullYear());
	var daysGrid = makeDaysGrid(startDayOfWeek,day,intca_daysInMonth,newCal,eltName);
	if (isIE) {
		var elt = document.all[eltName];
		if(elt.style.display == 'none') {
		        elt.style.display='';
		        hElement("SELECT");
		        hElement("OBJECT");
		        hElement("IFRAME");
		}
		elt.innerHTML = daysGrid;
	} else {
		var elt = document.layers[eltName].document;
		elt.open();
		elt.write(daysGrid);
		elt.close();
	}
}

function incMonth(delta,eltName) {
	ca_displayMonth += delta;
	if (ca_displayMonth >= 12) {
		ca_displayMonth = 0;
		incYear(1,eltName);
	} else if (ca_displayMonth <= -1) {
		ca_displayMonth = 11;
		incYear(-1,eltName);
	} else {
		newCalendar(eltName);
	}
}

function incYear(delta,eltName) {
	ca_displayYear = parseInt(ca_displayYear + '') + delta;
	newCalendar(eltName);
}

function makeDaysGrid(startDay,day,intca_daysInMonth,newCal,eltName) {
	var daysGrid;
	var month = newCal.getMonth();
	var year = newCal.getFullYear();
	var isThisYear = (year == new Date().getFullYear());
	var isThisMonth = (day > -1)
	daysGrid = '<table border=0 cellspacing=0 cellpadding=1><tr><td colspan=7 bgcolor="SteelBlue" class=subsys-title nowrap>';
	daysGrid += '<font face="courier new, courier" size=2>';
	daysGrid += '<a href="javascript:incMonth(-1,\'' + eltName + '\')" title="\u4e0a\u500b\u6708"><img src="../../images/calandar/mm_prev.gif" name="\u4e0a\u500b\u6708" border="0"></a>';

	daysGrid += '<b>';
	if (isThisMonth) { daysGrid += '<font color=red>' + ca_months[month] + '</font>'; }
	else { daysGrid += '<font color=White>' + ca_months[month] + '</font>'; }
	daysGrid += '</b>';

	daysGrid += '<a href="javascript:incMonth(1,\'' + eltName + '\')" title="\u4e0b\u500b\u6708"><img src="../../images/calandar/mm_next.gif" name="\u4e0b\u500b\u6708" border="0"></a>';
	daysGrid += '&nbsp;&nbsp;&nbsp;';
	daysGrid += '<a href="javascript:incYear(-1,\'' + eltName + '\')" title="\u53bb\u5e74"><img src="../../images/calandar/yy_prev.gif" name="\u53bb\u5e74" border="0"></a>';

	daysGrid += '<b>';
	//sYear = ((year-1911)<100) ? "0"+(year-1911) : ""+(year-1911) ; 
	sYear = year; 
	if (isThisYear) { daysGrid += '<font color=red>' + sYear + '</font>'; }
	else { daysGrid += '<font color=White>' + sYear + '</font>'; }
	daysGrid += '</b>';

	daysGrid += '<a href="javascript:incYear(1,\'' + eltName + '\')" title="\u660e\u5e74"><img src="../../images/calandar/yy_next.gif" name="\u660e\u5e74" border="0"></a>';
	daysGrid += '&nbsp;&nbsp;';
	daysGrid += '<a href="javascript:ca_hideElement(\'' + eltName + '\')" title="\u95dc\u9589\u8996\u7a97"><img src="../../images/calandar//exit.gif" name="\u96e2\u958b" border="0"></a></td></tr>';
	daysGrid += '<tr class=function-bar><td>\u65e5</td><td>\u4e00</td><td>\u4e8c</td><td>\u4e09</td><td>\u56db</td><td>\u4e94</td><td>\u516d</td></tr><tr>';
	var dayOfMonthOfFirstSunday = (7 - startDay + 1);
	for (var intWeek = 0; intWeek < 6; intWeek++) {
		var dayOfMonth;
		for (var intDay = 0; intDay < 7; intDay++) {
			dayOfMonth = (intWeek * 7) + intDay + dayOfMonthOfFirstSunday - 7;
			if (dayOfMonth <= 0) {
				daysGrid += "<td>\u3000</td>";
			} else if (dayOfMonth <= intca_daysInMonth) {
				var color = "blue";
				if (day > 0 && day == dayOfMonth) color="red";
				daysGrid += '<td><a href="javascript:setDay(';
				daysGrid += dayOfMonth + ',\'' + eltName + '\')" '
				daysGrid += 'onmouseover="this.style.backgroundColor=\'SteelBlue\';this.style.color=\'White\';"';
				daysGrid += 'onmouseout="this.style.backgroundColor=\'White\';this.style.color=\''+color+'\';"';
				daysGrid += 'style="color:' + color + '">';
				var dayString = dayOfMonth + "</a></td>";
				if (dayString.length == 10) dayString = '0' + dayString;
				daysGrid += dayString;
			}
		}
		if (dayOfMonth < intca_daysInMonth) daysGrid += "</tr>";
	}
	return daysGrid + "</td></tr></table>";
}

function hElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;

		// Find the element's offsetTop and offsetLeft relative to the BODY tag.
		objLeft   = obj.offsetLeft;
		objTop    = obj.offsetTop;
		objParent = obj.offsetParent;
		while (objParent.tagName.toUpperCase() != "BODY")
		{
			objLeft  += objParent.offsetLeft;
			objTop   += objParent.offsetTop;
			objParent = objParent.offsetParent;
		}
		// Adjust the element's offsetTop relative to the dropdown menu
		objTop = objTop - window.event.y;

		if (window.event.x > (objLeft + obj.offsetWidth) || objLeft > (window.event.x + document.all("Calendar").offsetWidth))
			;
		//else if (objTop > ToolbarMenu.offsetHeight)
		//	;
		else
			obj.style.visibility = "hidden";
	}
}

function sElement(elmID)
{
	for (i = 0; i < document.all.tags(elmID).length; i++)
	{
		obj = document.all.tags(elmID)[i];
		if (! obj || ! obj.offsetParent)
			continue;
		obj.style.visibility = "";
	}
}

function setDay(day,eltName) {
        var mm = (ca_displayMonth + 1) + "";
        if(mm.length == 1) mm = '0' + mm;
        
        var dd = (day < 10)? "0" + day : "" + day;
		var yy = parseInt(ca_displayYear); //-1911;
		//var yy = ca_displayYear;
	document.all(old_colname).focus();
	ca_displayElement.value = yy +'-'+ mm +'-'+ dd;
	ca_hideElement(eltName);
}

function ca_hideElement(eltName) {
	document.all(eltName).style.display = 'none';
	sElement("SELECT");
	sElement("OBJECT");
	sElement("IFRAME");
}
